<?php
/**
 * TomatoCMS
 *
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 *
 * @copyright    Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license        http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version     $Id: Attribute.php 2870 2010-05-17 03:04:13Z huuphuoc $
 * @since        2.0.5
 */

class Shop_Models_Dao_Pdo_Mysql_Attribute extends Tomato_Model_Dao
    implements Shop_Models_Interface_Attribute
{
    public function convert($entity)
    {
        return new Shop_Models_Attribute($entity);
    }

    /**QuanVo modified at here*/
    public function find($offset, $count, $exp = null)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('a' => $this->_prefix . 'shop_attribute_type'));
        if ($exp) {
            if (isset($exp['attribute_type_admin_name'])) {
                $select->where("a.attribute_type_admin_name LIKE '%". addslashes($exp['attribute_type_admin_name']) . "%'");
            }
            if (isset($exp['category_id'])) {
                $select->where("a.category_id LIKE '%,". addslashes($exp['category_id']) . ",%'");
            }
        }
        $rs = $select->order('a.attribute_type_id DESC')
                    ->limit($count, $offset)
                    ->query()
                    ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function count($exp = null)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('a' => $this->_prefix . 'shop_attribute_type'), array('num_attribute' => 'COUNT(*)'));
        if ($exp) {
            if (isset($exp['attribute_type_admin_name'])) {
                $select->where("a.attribute_type_admin_name LIKE '%". addslashes($exp['attribute_type_admin_name']) . "%'");
            }
            if (isset($exp['category_id'])) {
                $select->where("a.category_id LIKE '%,". addslashes($exp['category_id']) . ",%'");
            }
        }
        return $select->query()->fetch()->num_attribute;
    }

    public function add($attribute)
    {
        $this->_conn->insert($this->_prefix.'shop_attribute_type', array(
                'attribute_type_admin_name' => $attribute->attribute_type_admin_name,
                'category_id'               => $attribute->category_id,
        ));
        return $this->_conn->lastInsertId($this->_prefix.'shop_attribute_type');
    }

    public function getAttributeContentById($id)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('c' => $this->_prefix.'shop_attribute_type_content'))
                        ->where('c.attribute_type_id = ?', $id)
                        ;
        $rs = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function getAttributeById($id)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('c' => $this->_prefix.'shop_attribute_type'))
                        ->where('c.attribute_type_id = ?', $id)
                        ->limit(1);
        $rs = $select->query()->fetchAll();
        $attribute = new Tomato_Model_RecordSet($rs, $this);
        return (count($attribute) == 0) ? null : $attribute[0];
    }

    public function update($attribute)
    {
        $where[] = 'attribute_type_id = '.$this->_conn->quote($attribute->attribute_type_id);
        $this->_conn->update($this->_prefix.'shop_attribute_type', array(
                'attribute_type_admin_name'    => $attribute->attribute_type_admin_name,
                'category_id'                  => $attribute->category_id,
        ), $where);
    }

    public function addContent($attribute)
    {
        $this->_conn->insert($this->_prefix.'shop_attribute_type_content', array(
                'attribute_type_id'                   => $attribute->attribute_type_id,
                'attribute_type_content_name'      => $attribute->attribute_type_content_name,
                'attribute_type_content_description'      => $attribute->attribute_type_content_description,
                'language_code'                  => $attribute->language_code,
            ));
        return $this->_conn->lastInsertId($this->_prefix.'shop_attribute_type_content');
    }

    public function updateContent($attribute)
    {
        $where[] = 'attribute_type_content_id = '.$this->_conn->quote($attribute->attribute_type_content_id);
        $this->_conn->update($this->_prefix.'shop_attribute_type_content', array(
                'attribute_type_content_name'         => $attribute->attribute_type_content_name,
                'attribute_type_content_description'         => $attribute->attribute_type_content_description,
        ), $where);
    }

    public function getAttributeTypeByProductId($product_id)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('pa' => $this->_prefix.'shop_product_attribute'))
                        ->joinInner(array('at' => $this->_prefix.'shop_attribute_type'), "pa.attribute_type_id = at.attribute_type_id")
                        ->where('pa.product_id = ?', $product_id)
                        ;
        $rs = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function getValueTypeIdChainByAttributeTypeId($attribute_type_id,$product_id)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('at' => $this->_prefix.'shop_product_attribute'))
                        ->where('at.attribute_type_id = ?', $attribute_type_id)
                        ->where('at.product_id = ?', $product_id)
                        ->limit(1);
        $rs = $select->query()->fetchAll();
        $attribute = new Tomato_Model_RecordSet($rs, $this);
        return (count($attribute) == 0) ? null : $attribute[0];
    }

    public function updateValueTypeIdChainByAttributeTypeId($attribute_type_id,$str_value_type_chain,$product_id)
    {
        $where[] = 'attribute_type_id = '.$attribute_type_id.' and product_id = '.$product_id;
        $this->_conn->update('shop_product_attribute', array(
                'value_type_id_chain'                     => $str_value_type_chain,
        ), $where);
    }

    public function countAttribute($attribute_type_id)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('pa' => $this->_prefix.'shop_product_attribute'), array('num_att' => 'COUNT(*)'))
                        ->where('pa.attribute_type_id = ?', $attribute_type_id)
                        ;
        $row = $select->query()->fetch();
        return $row->num_att;
    }

    public function delete($id){
        $where[] = 'attribute_type_id = '.$this->_conn->quote($id);
        $this->_conn->delete($this->_prefix.'shop_attribute_type_content', $where);
        return $this->_conn->delete($this->_prefix.'shop_attribute_type', $where);
    }

    /**========================Front end==========================================*/

    public function getAttOfPro($id){
        $select = $this->_conn
                        ->select()
                        ->from(array('pa' => $this->_prefix.'shop_product_attribute'))
                        ->joinInner(array('at' => $this->_prefix.'shop_attribute_type'), "pa.attribute_type_id = at.attribute_type_id", array('attribute_type_id', 'attribute_type_admin_name'))
                        ->joinLeft(array('atc' => $this->_prefix.'shop_attribute_type_content'), "at.attribute_type_id = atc.attribute_type_id AND atc.language_code = '{$this->_lang}'", array('attribute_type_content_id', 'attribute_type_content_name', 'attribute_type_content_description', 'language_code'))
                        ->where('pa.product_id = ?', $id)
                        ;
        $rs = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function getFEById($id)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('at' => $this->_prefix.'shop_attribute_type'),array('attribute_type_id', 'attribute_type_admin_name'))
                        ->joinLeft(array('atc' => $this->_prefix.'shop_attribute_type_content'), "at.attribute_type_id = atc.attribute_type_id AND atc.language_code = '{$this->_lang}'", array('attribute_type_content_id', 'attribute_type_content_name', 'attribute_type_content_description', 'language_code'))
                        ->where('at.attribute_type_id = ?', $id)
                        ->limit(1);
        $rs = $select->query()->fetchAll();
        $attribute = new Tomato_Model_RecordSet($rs, $this);
        return (count($attribute) == 0) ? null : $attribute[0];
    }

    public function getAllAtt(){
        $select = $this->_conn
                        ->select()
                        ->from(array('at' => $this->_prefix.'shop_attribute_type'), array('attribute_type_id', 'attribute_type_admin_name'))
                        ->joinLeft(array('atc' => $this->_prefix.'shop_attribute_type_content'), "at.attribute_type_id = atc.attribute_type_id AND atc.language_code = '{$this->_lang}'", array('attribute_type_content_id', 'attribute_type_content_name', 'attribute_type_content_description', 'language_code'))
                        ;
        $rs = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }
}

